Skip to content

Unified: Support Swift subscript calls and declarations - #23111

Draft
asgerf wants to merge 1 commit into
github:mainfrom
asgerf:asgerf-swift-subscript-support
Draft

asgerf wants to merge 1 commit into
github:mainfrom
asgerf:asgerf-swift-subscript-support

Conversation

@asgerf

@asgerf asgerf commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

xs[i] is now seen as a call to a member named subscript, i.e. xs.subscript(i).

A subscript declaration can contain a getter and a setter, with a shared parameter list:

class C {
  subscript(x: Int) -> Int {
    get { .. }
    set { ... }
  }

The getter is extracted as a FunctionDeclaration named subscript, and the setter becomes an AccessorDeclaration with the accessor-kind setter_method. We clone the parameter list so both callables get their Parameter (crude, but gets the job done). The setter is recognised on the QL side as a SetterMethodDeclaration.

When a call appears as an assignment target, we use the existing TSetterCall branch of DataFlowCall to represent the call to a SetterMethodDeclaration. For type inference, we just let the call resolve as normal (resolves to the getter).

Relationship to Ruby

My thinking is that perhaps Ruby's foo= methods can also be handled by being mapped to "setter methods". I've been going back and forth on the idea of fully adopting Ruby's convention, and representing the subscript-setter as a method named subscript=, but eventually decided against it as it lead to the proliferation of ad-hoc string manipulation in the codebase.

@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch 4 times, most recently from 8a57ce8 to b4ac4b1 Compare November 16, 2026 10:44
}

/** Gets the node corresponding to the field `type_parameter`. */
final F::TypeParameter getTypeParameter(int i) {
* The new value is passed as an extra positional argument after the explicit ones.
*/
pragma[nomagic]
private predicate setterCallSelector(CallExpr call, string name, string argLabels) {
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from 5c9f617 to ff2cdda Compare November 18, 2026 12:49
Extract subscript accesses as calls to a 'subscript' member, and subscript
declarations as a 'subscript' getter method followed by a 'subscript'
accessor of kind 'setter_method'. The setter clones the subscript parameters
and takes the new value as an extra trailing positional parameter.

A call appearing as an assignment target, or written back after an in-place
update, now invokes the corresponding setter method, generalizing the
existing property setter call logic.

MaD models refer to setter methods as 'name:set', with the value as the last
argument label, e.g. 'subscript:set(_:_:)'.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from ff2cdda to 7ecb52d Compare November 18, 2026 13:07

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants